package com.bstek.uflo.form.view.table;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Page;
import com.bstek.uflo.command.CommandService;
import com.bstek.uflo.form.command.GenerateTablesCommand;
import com.bstek.uflo.form.command.GetDataTypeCommand;
import com.bstek.uflo.form.command.QueryTableColumnCommand;
import com.bstek.uflo.form.command.QueryTableCommand;
import com.bstek.uflo.form.command.SaveTableCommand;
import com.bstek.uflo.form.model.MappingProvider;
import com.bstek.uflo.form.model.TableColumn;
import com.bstek.uflo.form.model.TableDefinition;

/**
 * @author Jacky.gao
 * @since 2013年11月7日
 */
@Component("uflo.form.tableMaintain")
public class TableMaintain implements ApplicationContextAware{
	@Autowired
	@Qualifier(CommandService.BEAN_ID)
	private CommandService commandService;
	private ApplicationContext applicationContext;
	@DataProvider
	public void loadTables(Page<TableDefinition> page,Long parentId){
		commandService.executeCommand(new QueryTableCommand(page,parentId));
	}
	
	@DataProvider
	public List<TableColumn> loadColumns(Long tableId){
		return commandService.executeCommand(new QueryTableColumnCommand(tableId));
	}
	
	@DataProvider
	public List<Map<String,Object>> getDataTypes(){
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		List<String> types=commandService.executeCommand(new GetDataTypeCommand());
		if(types==null)return list;
		for(String type:types){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("name", type);
			list.add(map);
		}
		return list;
	}
	
	@DataProvider
	public Collection<Map<String,Object>> loadMappingProviders(){
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		for(String name:applicationContext.getBeansOfType(MappingProvider.class).keySet()){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("name", name);
			list.add(map);
		}
		return list;
	}
	
	@DataResolver
	public void saveTables(Collection<TableDefinition> tables){
		commandService.executeCommand(new SaveTableCommand(tables));
	}
	
	@DataResolver
	public void generateTables(Collection<TableDefinition> tables){
		if(tables==null || tables.size()==0){
			throw new IllegalArgumentException("请指定要生成表的表定义信息！");
		}
		TableDefinition table=tables.iterator().next();
		commandService.executeCommand(new GenerateTablesCommand(table));
	}
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext=applicationContext;
	}
}
